class AttackTo extends Action {
	constructor(enity, target, callback) {
		super();
		
		this.enity = enity;
		this.target = target;
        this.callback = callback;
	}
	
	async update() {
        // 如果自身死亡则停止动作
        if(this.enity.hp <= 0) {
            this.callback(this);
            return;
        }

		// 检查与目标点距离
		const xDiff = this.target.x - this.enity.x;
		const yDiff = this.target.y - this.enity.y;
		const distance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
		
		// 如果小于自身攻击范围的话进行攻击
		if(distance < this.enity.scene.gridSize * 2) {		
			// 执行计算面对朝向
			updateEnityDirection(this.enity, xDiff == 0 && yDiff == 0 ? 0 : Math.atan2(xDiff, yDiff));

			// 执行攻击动作
            await this.enity.attack();
		}
		else {
			// 更新移动逻辑
			new MoveTo(this.enity, this.enity.scene.findPath(this.enity.position, this.target.position)).update();
		}	
	}
}